package cn.ywang;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Auther: BeatificWang
 * @Date: 2018/11/3 9:51
 */
@Slf4j
public class Server {
	private static final int DEFAULT_PORT = 8888;

	private static ServerSocket serverSocket;

	public static void start() {
		start(DEFAULT_PORT);
	}

	private static void start(int port) {
		if (serverSocket != null) {
			return;
		}
		try {
			serverSocket = new ServerSocket(port);
			log.info("服务端已启动，端口号为：" + port);
			System.out.println("服务端已启动，端口号为：" + port);
			while (true) {
				Socket socket = serverSocket.accept();
				new Thread(new ServerHandler(socket)).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
